<?php
/**
 * 支付推送模型
 * @author  wyb <[<email address>]>
 */
namespace Qwadmin\Model;
use Think\Model;
class SendPayServerModel extends Model {

	public function __construct() {

	}

	/**
	 * 订单信息推送
	 */
	public function paySend( $order_id ) {
		if( !$order_id ) return '';
		$order_data = M('order')->alias('o')->join('__ORDER_PAYMENT__ op ON op.order_id = o.id')->where(array('o.id' => $order_id,'o.state' => 1))
								->field('o.notify_url,o.channel_code,o.ref AS out_trade_no,op.payamount AS total_fee,op.payment_no,op.payment_account AS payment')
								->find();
		//有订单信息
		if( $order_data ) {
			$data = array(
				'pay_state' => 'success',
				'time_end'	=> date('Ymdhis'),
				'out_trade_no' => $order_data['out_trade_no'],
				'total_fee'	=> $order_data['total_fee'],
				'payment_no'	=> $order_data['payment_no'],
				'payment'	=> $order_data['payment']
			);
			$customer = M('customer')->where(array('code' => $order_data['channel_code']))->find();
			if( !$customer ) return;
			$data['app_id'] = $customer['id'];
			$data['aign'] = \Api\Server\TrilateralCheckServer::init()->aign( $data , $customer['key'] );
			$result = \Api\Server\HttpServer::init()->send( $order_data['notify_url'] , $data );
			extract( $result );
			//成功推送至客户服务器
			if( $status['http_code'] >= 200 && $status['http_code'] < 300  && $result ) {
				$result = strtolower( trim( $result ) );
				//客户端处理成功
				if( $result == 'success' ) {
					D('Common/OrderPaySend')->saveOrderSendInfo($order_id , 1);
				} else {
					D('Common/OrderPaySend')->saveOrderSendInfo($order_id , 2 , '对方服务器业务处理失败，返回“error”。');
				}
			} else {
				D('Common/OrderPaySend')->saveOrderSendInfo($order_id , 3);
			}
		}
	}

	/**
	 * 订单退款通知
	 */
	public function refundNotifySend( $order_id ) {
		$order_data = M('order')->find($order_id);
		if( $order_data ) {
			D('Common/OrderPaySend')->refundSendInfo( $order_data );
		}
	}

}